package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ChildLevelAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.ParentLevelAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class PickTwoLevelPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private TextView lineView;
    private LinearLayout bottomLayout;
    private ParentLevelAdapter parentAreaAdapter;
    private ChildLevelAdapter childAreaAdapter;
    private List<ParentAreaEntity> selectedEntities;
    private List<ParentAreaEntity<List<ParentAreaEntity>>> areaEntityList;
    private String parentNo = "";
    private String childNo = "";
    private String parentName = "";
    private String childName = "";
    private TwoAreaPopWindow.OnCloseBack onCloseBack;
    ParentAreaEntity<List<ParentAreaEntity>> parentEntity = new ParentAreaEntity<>();
    ParentAreaEntity childEntityResult = new ParentAreaEntity();

    public PickTwoLevelPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_two_level;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        bottomLayout = holder.getPop().findViewById(R.id.ll_next);
        okView = holder.getPop().findViewById(R.id.tv_ok);
        resetView = holder.getPop().findViewById(R.id.tv_next);
        bgView = holder.getPop().findViewById(R.id.cover_layout);
        lineView = holder.getPop().findViewById(R.id.line_view);
        bgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(parentEntity, childEntityResult, childName);
                }
            }
        });

        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomLayout.setVisibility(View.GONE);
                recyclerView.setAdapter(parentAreaAdapter);
                parentAreaAdapter.setDatas(areaEntityList);
            }
        });
        parentAreaAdapter = new ParentLevelAdapter(mContext, null);
        parentAreaAdapter.setItemSelect(true);
        parentAreaAdapter.setDefItem(0);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(parentAreaAdapter);
        parentAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity<List<ParentAreaEntity>>>() {
            @Override
            public void onItemClick(ParentAreaEntity<List<ParentAreaEntity>> listParentAreaEntity, int position) {

                parentEntity = listParentAreaEntity;

                if (listParentAreaEntity.getName().equals("不限")) {
                    dissMiss();
                    if (onResultClick != null) {
                        ParentAreaEntity<List<ParentAreaEntity>> parentEntity1 = new ParentAreaEntity<>();
                        ParentAreaEntity childEntityResult1 = new ParentAreaEntity();
                        parentEntity1.setName("不限");
                        parentEntity1.setNo("");
                        parentEntity1.setId("");
                        childEntityResult1.setName("不限");
                        childEntityResult1.setNo("");
                        childEntityResult1.setId("");
                        onResultClick.result(parentEntity1, childEntityResult1, "不限");
                    }
                } else {
                    if (parentName != null && !parentName.equals(listParentAreaEntity.getName())) {
                        childAreaAdapter.setDefItem(0);
                        childAreaAdapter.resetCheckDatas();
                    }
                    parentNo = listParentAreaEntity.getNo();
                    parentName = listParentAreaEntity.getName();
//                no = listParentAreaEntity.getNo();
                    lineView.setVisibility(View.VISIBLE);
                    bottomLayout.setVisibility(View.VISIBLE);
                    recyclerView.setAdapter(childAreaAdapter);

                    List<ParentAreaEntity> list = listParentAreaEntity.getChildren();
                    if (list.size() == 0 || !list.get(0).getName().equals("不限")) {
                        ParentAreaEntity entity = new ParentAreaEntity();
                        entity.setNo("");
                        entity.setName("不限");
                        entity.setId("");
                        list.add(0, entity);
                    }
                    childAreaAdapter.setDatas(list);
                    if (list != null && list.size() > 0) {
                        childEntityResult = list.get(0);
                    }
                }
            }
        });

        childAreaAdapter = new ChildLevelAdapter(mContext, null);
        childAreaAdapter.setItemSelect(true);
        childAreaAdapter.setDefItem(0);
        childAreaAdapter.setMultiSelect(false);
        childAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity>() {
            @Override
            public void onItemClick(ParentAreaEntity childEntity, int position) {
                childNo = childEntity.getNo();
                childName = childEntity.getName();
                childEntityResult = childEntity;
                if (childName.equals("不限")) {
                    childEntityResult.setName("不限");
                    childEntityResult.setNo("");
                    childEntityResult.setId("");
                }
            }
        });
    }

    public void setData(List<ParentAreaEntity<List<ParentAreaEntity>>> areaEntityList, boolean isChange) {
        this.areaEntityList = areaEntityList;
        parentAreaAdapter.setDatas(areaEntityList);
        if (isChange) {
            bottomLayout.setVisibility(View.GONE);
            lineView.setVisibility(View.GONE);
            parentAreaAdapter.resetCheckDatas();
            recyclerView.setAdapter(parentAreaAdapter);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onCloseBack != null) {
            onCloseBack.dismiss();
        }
    }

    public interface OnCloseBack {
        void dismiss();
    }

    public void setOnCloseBack(TwoAreaPopWindow.OnCloseBack onCloseBack) {
        this.onCloseBack = onCloseBack;
    }
}
